<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<div class="sugg_list" v-if="searchResults.length !== 0">
			<div class="sugg_item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item)">
				<div class="goods_name">
					{{item.goods_name}}
				</div>
				<uni-icons type="right" size="16"></uni-icons>
			</div>
		</div>
		<!-- 搜索历史 -->
		<div class="history-box" v-else>
			<!-- 标题区 -->
			<div class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</div>
			<!-- 列表区 -->
			<div class="history-list">
				<uni-tag :text="item" v-for="(item,i) in historys" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</div>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null, //延时器timerId
				kw: '', //搜索关键词
				searchResults: [], //搜索的结果列表
				historyList: [] //搜索历史的数组
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			// input事件处理函数 防抖原理：不停地清除延时器,500毫秒内输完了就打印，没输完还在连续输入就清除原来延时器，新开延时器
			input(e) {
				//console.log(e)
				clearTimeout(this.timer) //如果用户在500毫秒内连续输入内容，清除之前的延时器

				// 重新开启一个延时器setTimeout里面放一个箭头函数setTimeout(() => {},500)让其500毫秒后去打印，开了延时器就会有返回值，并把timeId赋值给this.timer
				this.timer = setTimeout(() => {
					//console.log(e)
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			async getSearchList() {
				if (this.kw.length === 0) {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
				this.saveSearchResults()
			},
			gotoDetail(item) {
				console.log(item.goods_id)
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			saveSearchResults() {
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)
				//console.log(set);
				set.delete(this.kw)
				set.add(this.kw)
				//浅拷贝，将一个类数组对象或者可遍历对象转换成一个真正的数组,也是ES6的新增方法
				this.historyList = Array.from(set)
				//console.log(this.historyList);
				// 对搜索历史数据进行持久化的存储
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
				//console.log(JSON.stringify(this.historyList));

			},
			clean() {
				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			gotoGoodsList(kw) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		},
		computed: {
			// 注意：由于数组是引用类型，所以不要直接基于原数组调用reverse方法，以免修改原数组中元素的顺序，而是新建一个内存无关的数组，再进行revers，这里用到浅拷贝
			historys() {
				return [...this.historyList].reverse()
			}

		}


	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.sugg_list {
		padding: 0 5px;

		.sugg_item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;

			.goods_name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
				color: #000;
				background-color: #efefef;
				border: none;
			}
		}
	}
</style>
